<html>
<head>
<meta charset="UTF-8">
<title>数据表模型信息列表</title>
	<link id="easyuiTheme" href="../../static/easyui/themes/bootstrap/easyui.css" rel="stylesheet" type="text/css"/>
	<link href="../../static/easyui/themes/icon.css" rel="stylesheet" type="text/css"/>
	<script src="../../static/easyui/jquery.min.js" type="text/javascript"></script>
	<script src="../../static/easyui/jquery.cookie.js" type="text/javascript"></script>
	<script src="../../static/easyui/ui-theme.js" type="text/javascript"></script>
	<script src="../../static/easyui/jquery.easyui.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../static/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
	<script src="../../static/plugin/assets/jslib.util.js" type="text/javascript"></script>
	<script src="../../static/plugin/assets/jslib.jquery.ajax.js" type="text/javascript"></script>
	<script src="../../static/plugin/assets/jslib.easyui.util.js" type="text/javascript"></script>
	<script src="../../ffres/dict/all.js" type="text/javascript"></script>
	<link href="../../static/easyui/themes/frameless.css" rel="stylesheet" type="text/css"/>
	<a href="" target="_blank" id="commonLink" name="commonLink"></a>
	<style>
		<!-- 外层 -->
		.tba,.tba th,.tba td{text-align:center;border:none;border-collapse:collapse;}
		.tba td,.tba th{min-width:30px;height: 30px;border:none;}
		.tba td{vertical-align:top;}
		<!-- 详细信息 -->
		.detail{width:100%;}
		.detail th{font-weight:normal;font-size:15px;background-color: #5173a0;color:white}
		.detail th.title,.detail td.title{height:30px;min-width:120px;white-space:nowrap;}
		.detail td.content{height:30px;min-width:80px;white-space:nowrap;}
		.detail,.detail th,.detail td{text-align:center;border: 1px solid #bcc1c2;border-collapse:collapse;}
		<!-- 外层 -->
		.detail-outer,.thum-outer th,.thum-outer td{border-collapse:collapse;}
		.detail-outer td,.detail-outer th{height: 30px}
		.detail-outer{width:100%;}
		<!-- 缩略信息 -->
		.thum{width:100%;}
		.thum th{font-weight:normal;font-size:15px;background-color: #5173a0;color:white}
		.thum th,.thum td{height:30px;min-width:120px;max-width:120px;white-space:nowrap;}
		.thum,.thum th,.thum td{vertical-align: middle;text-align:center;border: 1px solid #bcc1c2;border-collapse:collapse;}

		td.item{cursor: hand;}
		td.item:hover{background-color: #ddecfc}
		.selected{background-color: #cde4fc;}
	</style>
</head>
<script type="text/javascript">

	var packageId = $.getUrlParam('packageId');
	var projectId = $.getUrlParam('projectId');
	var allFields = {};

	$(function() {
		$('#dg-list').datagrid({
	        onDblClickRow: function (rowIndex, rowData) {
				doView();
	        }
	    });
		inite();
	});

	function inite(){
		$.restfulPostJson('../../api/model/table/infos?packageId='+packageId,null,function(data){
			$("#dg-list").datagrid({data: data.rows});
			createTableHtml(data.rows);
		})
	}

	function createTableHtml(rows){
		$("#dg-thumbnail").empty();
		$("dg-detailed").empty();
		var length=rows.length;
		var thumRows=Math.ceil(length/4)>0?Math.ceil(length/4):1;
		//生成缩略表格
		if(rows.length==0){
			var html="<tr><td colspan='11'>模型领域表数据为空！</td></tr>";
			$("#dg-thumbnail").append(html);
			$("#dg-detailed").append(html);
		}else{
			var thumHtml="<tr>";
			for(var i=0;i<thumRows*4;i++){
				if(i%4 != 0){
					thumHtml+="<td></td>";
				}
				var thumTable="";
				if(i<length){
					getTableFields(rows[i].objectId);
					thumTable+=createThumbnail(rows[i],i);
				}
				thumHtml+="<td>"+thumTable+"</td>";
				if((i+1)%4 == 0){
					thumHtml+="</tr></tr><tr><td colspan='11'></td></tr>";
					$("#dg-thumbnail").append(thumHtml);
					thumHtml="<tr>";
				}
			}
			//生成详细表格
			for(var i in rows){
				var detailHtml="<tr>";
				detailHtml+="<td>"+createDetailed(rows[i],i)+"</td>"
				detailHtml+="</tr><tr><td colspan='11'></td></tr>";
				$("#dg-detailed").append(detailHtml);
			}
		}
	}

	function createThumbnail(row,num){
		var fields=allFields[row.objectId];
		var pkLength=0;
		for(var i in fields){
			if(fields[i].isPk=="Y")
				pkLength++;
		}
		if(pkLength<4)
			pkLength=4;
		var html="<table class='thum'><thead><tr><th>领域模型</th><th>主键</th><tr></thead>";
		html+="<tbody><tr><td id='thum"+num+"' text='"+row.objectName+"' value='"+row.objectId+"' rowspan='"+(pkLength+1)+"' class='item' ondblclick='doEdit()'>"+row.objectName+"<br/>"+row.objectCode+"</td></tr>";
		for(var i=0;i<pkLength;i++){
			if(i<fields.length && fields[i].isPk=="Y"){
				html+="<tr><td valign='top' class='content item' ondblclick='doFieldUpdate(this)' id='"+fields[i].fieldId+"' value='"+fields[i].itemId+"'>"+fields[i].fieldName+"</td></tr>"
			}else{
				html+="<tr><td></td></tr>"
			}
		}
		html+="</tbody></table>";
		return html;
	}

	function queryForm(){
		var jsss= $("#searchForm").serializeJson();
		var jsonData= JSON.parse('{'+'"objectName":"'+jsss.objectName+'"' + ','+'"objectCode":"'+jsss.objectCode+'"'+'}');
		$.restfulPostForm('../../api/model/table/infos?packageId='+packageId,jsonData,function (data){
			$("#dg-list").datagrid({data: data.rows});
			createTableHtml(data.rows);
		})
	}

	function createDetailed(row,num){
		var fields=allFields[row.objectId];
		var detailRows=Math.ceil(fields.length/10)>0?Math.ceil(fields.length/10):1;
		var html="<table class='detail'><thead><tr><th class='title' style='width: 9%;'>领域模型</th><th style='width: 90%' colspan='10'>领域字段</th></tr></thead>";
		html+="<tbody><tr><td id='detail"+num+"' value='"+row.objectId+"' text='"+row.objectName+"' class='item title' rowspan='"+(detailRows+1)+"' ondblclick='doEdit()'>"+row.objectName+"<br/>"+row.objectCode+"</td></tr>";
		for(var i=0;i<detailRows*10;i++){
			if(i<fields.length){
				html+="<td class='content item' ondblclick='doFieldUpdate(this)' id='"+fields[i].fieldId+"' value='"+fields[i].itemId+"'>"+fields[i].fieldName+"</td>";
			}else{
				html+="<td class='content'></td>";
			}
			if((i+1)%10 == 0 && (i+1)==fields.length){
				html+="</tr>";

			}else if((i+1)%10 == 0){
				html+="<tr></tr>";
			}
		}
		html+="</tbody></table>";
		return html;
	}

	function doFieldUpdate(e){
		var itemId=e.attributes.value.value;
		var fieldId=e.id;
		JLEUtil.createModalWin('修改字段', 'ModelTableFieldUpdateNew.html?' + 'fieldId=' + fieldId + '&itemId=' + itemId);
	}

	function getTableFields(objectId){
		$.restfulPostForm('../../api/model/table/fields', {objectId: objectId},function(data){
			allFields[objectId]=data.rows;
		})
	}

	function getSelectItem(){
		if($('#dg-list').datagrid('getSelected')){
			return $('#dg-list').datagrid('getSelected');
		}else if($("#dg-detailed td.selected")[0] != null){
			var objectId=$("#dg-detailed td.selected")[0].attributes.value.value;
			var objectName=$("#dg-detailed td.selected")[0].attributes.text.value;
			return {objectId:objectId,objectName:objectName};
		}else if($("#dg-thumbnail td.selected")[0] != null){
			var objectId=$("#dg-thumbnail td.selected")[0].attributes.value.value;
			var objectName=$("#dg-thumbnail td.selected")[0].attributes.text.value;
			return {objectId:objectId,objectName:objectName};
		}else{
			return false;
		}
	}

	$(document).ready(function(){
		$(".item").click(function(e){
			$("td").removeClass("selected");
			$("#"+e.target.id).addClass("selected");
		});
	});

	function doAdd(){
		JLEUtil.createModalWin('新增', 'ModelTableInfoAdd.html?projectId=' + projectId + "&packageId=" + packageId);
	}
	
	function doUpdate() {
		var row = getSelectItem();
		if (row){
			JLEUtil.createModalWin('修改', 'ModelTableInfoUpdate.html?' + 'objectId=' + row.objectId);
		} else {
			$.messager.alert('提示','请选择表模型！','info');
		}
	}
	
	function doEdit() {
		var row = getSelectItem();
		if (row){
			var url = "ModelTableFieldList.html?objectId=" + row.objectId + "&objectName=" + encodeURI(encodeURI($.getUrlParam("packageName")+"-"+row.objectName));
			JLUtil.linkNewPage('../esaas-dmcenter/' + url);
		} else {
			$.messager.alert('提示','请选择表模型！','info');
		}
	}
	
	function doDelete(){
		var row = getSelectItem();
		if (row) {
			$.messager.confirm('删除提示', '你确定要删除表模型吗?', function(r){
				if (r){
					var jsonData = JSON.parse('{'+'"objectId":"' + row.objectId + '"'+'}');
					$.restfulDelete('../../api/model/table/info', jsonData, function(data) {
						$.messager.alert('提示','表模型删除成功!','info');
						inite();
					});
				}
			});
		} else {
			$.messager.alert('提示','请选择表模型！','info');
		}
	}

	function doView(){
		var row = getSelectItem();
		if (row) {
			JLEUtil.createModalWin(row.objectName + '详情','ModelTableInfoView.html?' + '&objectId=' + row.objectId);
		} else {
			$.messager.alert('提示','请选择表模型！','info');
		}
	}
	
	
	function doFinishCopy(table){
		var tableModelId = table.objectId;
		$.restfulGet('../../api/model/table/copy/' + tableModelId + '/' + packageId, null, function(data) {
			reloadList();
			$.messager.alert('提示','已复制所选表模型到当前目录!', 'info')
		});
	}
	
	function doTableModelScript() {
		var row = getSelectItem();
		if (row) {
			JLEUtil.createModalWin('详情', 'TableModelScript.html?' + 'objectId=' + row.objectId);
		} else {
			$.messager.alert('提示', '请选择表模型！', 'info');
		}
	}

	function reloadPackageTree() {
		parent.reloadNavTree();
		JLEUtil.closewin();
	}

	function reloadList() {
		inite();
		JLEUtil.closewin();
	};

	function doMove() {
		var row = getSelectItem();
		if (row) {
			JLEUtil.createModalWin('选择移动目录', `popProjectPackage.html?projectId=${projectId}&callback=moveCallback`);
		}
	}

	function moveCallback(selection) {
		var row = getSelectItem();
		if (!row) {
			return;
		}
		var jsonData = {
			objectId : row.objectId,
			packageId : selection.id,
		};
		$.restfulPut('../../api/model/table/info', jsonData, function(data) {
			inite();
		});
	}

	function doExcelImport() {
		JLEUtil.createModalWin('新增', 'ModelTableInfoAdd-Excel.html?projectId=' + projectId + "&packageId=" + packageId);
	}
	function doSqlImport() {
		JLEUtil.createModalWin('新增', 'ModelTableInfoAdd-Sql.html?projectId=' + projectId + "&packageId=" + packageId);
	}
	function doCopyImport() {
		//此行为新增，用于行选择表模型，选择后再进行复制操作
		JLEUtil.createModalWin('选择要复制的表模型','AllModelTableInfoList.html?callBack=doFinishCopy');
	}
	function doVirtualCreate() {}

	function tableShowType(type){
		if(type==("list")){
			document.getElementById("div-list").style.display="block";
			$("#dg-list").datagrid("resize")
			document.getElementById("div-detailed").style.display="none";
			document.getElementById("div-thumbnail").style.display="none";
		}else if(type==("thumbnail")){
			$('#dg-list').datagrid('clearChecked');
			document.getElementById("div-list").style.display="none";
			document.getElementById("div-detailed").style.display="none";
			document.getElementById("div-thumbnail").style.display="block";
		}else{
			$('#dg-list').datagrid('clearChecked');
			document.getElementById("div-list").style.display="none";
			document.getElementById("div-detailed").style.display="block";
			document.getElementById("div-thumbnail").style.display="none";
		}
	}

	function doCheck() {
		var row = getSelectItem();
		if (!row) {
			$.messager.alert('提示', '请选择表模型库！');
		}
		$.ajax({
			url:'../../api/project/tm/checkall',
			type:"POST",
			data:JSON.stringify({ projectId : projectId,objectId:row.objectId}),
			contentType:"application/json;charset=utf-8",
			dataType:"json",
			async:true,
			success: function(data) {
				if (data.rows == '0') {
					$.messager.alert('提示', '表模型库校验通过！', 'info');
				} else {
					JLEUtil.createModalWin(row.objectName+'表模型库校验结果', 'ProjectTmCheckList.html?projectId='+projectId);
				}
			}
		});

	}

	//下载数据库表结构word
	function doGenerateWord(){
		window.location.href = '../../api/project/tableModel/word/downLoad/' + projectId;
	}
	//下载数据库表结构word
	function doGenerateExcel(){
		window.location.href = '../../api/project/tableModel/excel/downLoad/' + projectId;
	}
	//下载数据库脚本文件
	function doGenerateScript(){
		window.location.href = '../../api/project/ddlSqlScript/downLoad/' + projectId;
	}

</script>
</head>
<body class="easyui-layout">
<div data-options="region:'north',title:''" style=" background: #F4F4F4;overflow: hidden">
	<div data-options="region:'north',title:''" style="height: 38px; background: #F4F4F4;overflow: hidden">
		<form id="searchForm" style="padding: 1px 0px 0px 2px" >
			<table>
				<tr>
					<td>&nbsp;表模型代码：</td>
					<td><input name="objectCode" id="objectCode"/></td>
					<td>&nbsp;表模型名称：</td>
					<td><input name="objectName" id="objectName"/></td>
					<td><a class="easyui-linkbutton" href="javascript:void(0);" onclick="queryForm();">查找</a></td>
					<td><a class="easyui-linkbutton" href="javascript:void(0);" onclick="JLEUtil.clearForm('searchForm');">清空</a></td>
				</tr>
			</table>
		</form>
	</div>
	<div data-options="region:'center',split:false">
		<table id="dg" class="easyui-datagrid" title=""
			   data-options="singleSelect:true,collapsible:true" toolbar="#tb">
		</table>
	</div>
	<div id="tb" style="padding:1px">
		<div style="margin-bottom:5px">
			<a href="#" class="easyui-menubutton" data-options="menu:'#tableAddMenu',iconCls:'icon-add'" plain="true">创建表模型</a>
			<a href="javascript:doEdit()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">编辑表模型</a>
			<a href="javascript:doView()" class="easyui-linkbutton" iconCls="icon-ok" plain="true">查看表模型</a>
			<a href="#" class="easyui-menubutton" data-options="menu:'#managerMenu',iconCls:'icon-man'" plain="true">管理表模型</a>
			<a href="javascript:doTableModelScript()" class="easyui-linkbutton" iconCls="icon-more" plain="true">查看建表语句</a>
			<a href="#" class="easyui-menubutton" data-options="menu:'#displayType',iconCls:'icon-more'" plain="true">展示方式</a>
			<a href="javascript:doCheck()" class="easyui-linkbutton" iconCls="icon-ok" plain="true">领域表模型校验</a>
			<a href="#" class="easyui-menubutton" data-options="menu:'#exportMenu',iconCls:'icon-more'" plain="true">领域文档生成</a>
		</div>
	</div>
</div>
<div data-options="region:'center',split:false">
	<div id="div-list" style="width: 100%;height: 100%;display: none;">
		<table id="dg-list" class="easyui-datagrid" title="" style="width:100%;height:100%;"
			   data-options="singleSelect:true,collapsible:true" rownumbers="true" pagination="true" pageSize="20">
			<thead>
			<tr>
				<th data-options="field:'objectCode',resizable:true" width="15%">表模型代码</th>
				<th data-options="field:'objectName',resizable:true" width="21%">表模型名称</th>
				<th data-options="field:'objectState',resizable:true" width="10%" formatter="formatter_DM_OBJECT_STATE" hidden="true">表模型状态</th>
				<th data-options="field:'tableDataCycle',resizable:true" width="10%" formatter="formatter_DM_DATA_CYCLE">表数据周期</th>
				<th data-options="field:'createUser',resizable:true" width="10%">创建者</th>
				<th data-options="field:'createTime',resizable:true" width="13%">创建时间</th>
				<th data-options="field:'objectDesc',resizable:true" width="36%">表模型用途</th>
				<th data-options="field:'tableRecordLength',resizable:true" hidden="true">表记录长度</th>
			</tr>
			</thead>
		</table>
	</div>

	<div id="div-thumbnail" style="width: 98%;height: 100%;margin: 10px;">
		<table id="dg-thumbnail" class="tba">
		</table>
	</div>

	<div id="div-detailed" style="width: 98%;height: 100%;margin: 10px;display: none">
		<table id="dg-detailed" class="detail-outer">
		</table>
	</div>

</div>
<div id="exportMenu" style="width: 150px;">
	<div onclick="doGenerateWord()">生成Word文档</div>
	<div onclick="doGenerateExcel()">生成Excel文档</div>
	<div onclick="doGenerateScript()">生成数据库脚本</div>
	<!--<div onclick="doGeneratePDF()">生成PDF文档</div>-->
</div>
<div id="managerMenu" style="width:160px;">
		<div onclick="javascript:doUpdate()">表模型信息修改</div>
		<div onclick="javascript:doMove()">表模型移动到其它目录</div>
		<div onclick="javascript:doDelete()">表模型删除</div>
</div>
<div id="displayType" style="width:120px;">
	<div onclick="javascript:tableShowType('list')">列表信息</div>
	<div onclick="javascript:tableShowType('thumbnail')">缩略信息</div>
	<div onclick="javascript:tableShowType('detailed')">详细信息</div>
</div>
<div id="tableAddMenu" style="width:160px;">
	<div onclick="javascript:doAdd()">创建表模型</div>
	<div onclick="javascript:doExcelImport()">从Excel导入表模型</div>
	<div onclick="javascript:doSqlImport()">从建表语句导入表模型</div>
	<div onclick="javascript:doCopyImport()">复制指定表模型</div>
	<div onclick="javascript:doVirtualCreate()">创建虚拟表模型</div>
</div>
</body>
</html>

